package com.kevin.learn.leetcode;

import com.alibaba.fastjson.JSON;
import com.kevin.learn.domain.CareType;
import com.kevin.learn.util.DateUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.hutool.core.date.DateUtil;

public class DateDemo {
    public static void main(String[] args) {
        //        getMiddleDate(LocalDate.parse("2021-03-20 14:20:20",  DateTimeFormatter.ofPattern("yyyy-MM-dd")), LocalDate.parse("2021-03-20")).stream().forEach(System.out::println);

        Date startDate = DateUtil.parse("2021-04-10 12:00:00");
        Date endDate = DateUtil.parse("2021-04-12 11:00:00");

        HashMap<String, DayBean> map = new HashMap<>();
        List<String> middleDate = getMiddleDate(date2LocalDate(startDate), date2LocalDate(endDate));
        for (int i = 0; i < middleDate.size(); i++) {

            String localDate = middleDate.get(i);
            DayBean dayBean = new DayBean(DateUtil.parse(localDate + " 00:00:00"), DateUtil.parse(localDate + " 23:59:59"));
            if (i == 0) {
                dayBean.setStartTime(startDate);
            }
            if (i == middleDate.size() - 1) {
                dayBean.setEndTime(endDate);
            }
            map.put(localDate, dayBean);
        }

        for (Map.Entry<String, DayBean> entry : map.entrySet()) {
            DayBean dayBean = entry.getValue();
            dayBean.setStartTimeStr(DateUtil.format(dayBean.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
            dayBean.setEndTimeStr(DateUtil.format(dayBean.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
            entry.setValue(dayBean);
        }

        System.out.println(JSON.toJSON(map));

        // 查交集
        Set<String> set = getIntersection(middleDate);
        System.out.println(JSON.toJSON(set));

        BigDecimal totalDay = new BigDecimal(0);
        for (String s : set) {
            DayBean dayBean = map.get(s);
            BigDecimal bigDecimal = calcDays(dayBean.getStartTime(), dayBean.getEndTime(), 1);
            System.out.println("日期:" + s + " 天数:" + bigDecimal);
            totalDay = totalDay.add(bigDecimal);
        }
        System.out.println("总天数:" + totalDay);
    }

    public static BigDecimal calcDays(Date s, Date e, Integer careType) {
        boolean isOne2One = careType == CareType.ONE_2_ONE.getVal();
        BigDecimal natualDays = new BigDecimal(String.valueOf(DateUtils.getDiffNatualDay(e, s) + 1));
        if (isOne2One && DateUtils.getHour(s) >= 12) {
            // 1对1，如果开始时间是下午，则当天算半天（减0.5)
            natualDays = natualDays.subtract(new BigDecimal("0.5"));
        }

        if (DateUtils.getHour(e) < 12) {
            // 如果结束时间是上午，则只算半天
            natualDays = natualDays.subtract(new BigDecimal("0.5"));
        }
        return natualDays;
    }

    private static Set<String> getIntersection(List<String> middleDate) {
        Set<String> workDay = new HashSet<>(middleDate);

        Set<String> holidayDay = new HashSet<>();
        holidayDay.add("2021-04-10");
        holidayDay.add("2021-04-11");
        holidayDay.add("2021-04-12");

        workDay.retainAll(holidayDay);
        
        return workDay;
    }

    public static List<String> getMiddleDate(LocalDate begin, LocalDate end) {
        List<String> localDateList = new ArrayList<>();
        long length = end.toEpochDay() - begin.toEpochDay();
        for (long i = length; i >= 0; i--) {
            localDateList.add(end.minusDays(i).toString());
        }
        return localDateList;
    }

    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }


}
